
package web;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;

import org.primefaces.event.FileUploadEvent;

import construtora.Construtora;

import obra.Obra;
import obra.ObraRN;



@ManagedBean(name="obraBean")
@SessionScoped
public class ObraBean {

	private Obra	    obra	= new Obra();
	
	
	private List<Obra>	lista;
	private String	       destinoSalvar;
	private Obra obraSelecionada = new Obra();
	
	//private byte[] bfoto;
	private String teste;
	
	
	public String novo() {
		this.destinoSalvar = "/construtora/consultarObra";
		this.obra = new Obra();
		return "/construtora/cadastrarObra";
	}

	public String editar() {
		return "/construtora/cadastrarObra";
	}

	public String salvar() {
		//FacesContext context = FacesContext.getCurrentInstance();

		ContextoBean contextoBean = new ContextoBean();
		Construtora construtora = new Construtora();
		construtora = (Construtora) contextoBean.getUsuarioLogado();
		this.obra.setConstrutora(construtora);
		
		ObraRN obraRN = new ObraRN();
		//obra.setImagem(bfoto);
		obraRN.salvar(this.obra);
		
		return this.destinoSalvar;
	}

	public String excluir() {
		ObraRN obraRN = new ObraRN();
		obraRN.excluir(this.obra);
		this.lista = null;
		return null;
	}

		
	public List<Obra> getLista() {
		if (this.lista == null) {
			ContextoBean contextoBean = new ContextoBean();
			Construtora construtora = new Construtora();
			construtora = (Construtora) contextoBean.getUsuarioLogado();
			
			ObraRN obraRN = new ObraRN();
			this.lista = obraRN.listarObrasConstrutora(construtora);
		}
		return this.lista;
	}
	
	private String destination="C:\\JAVA\\PFC\\workspace\\acompanhe-sua-obra\\WebContent\\fotosObras\\";

    public void upload(FileUploadEvent event) {  
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }  

    public void copyFile(String fileName, InputStream in) {
           try {
             
        	   ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
                
        	   
        	   // write the inputStream to a FileOutputStream
                OutputStream out = new FileOutputStream(new File(extContext.getRealPath
                		("//fotosObras//" + fileName)));
             
                int read = 0;
                byte[] bytes = new byte[1024];
             
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                

                in.close();
                out.flush();
                out.close();
                

                System.out.println("New file created!");
                System.out.println(extContext.getRealPath("//fotosObras//" + fileName));
                } catch (IOException e) {
                System.out.println(e.getMessage());
                }
    }
	
	/*public void upload(FileUploadEvent event) {  
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }  

    public void copyFile(String fileName, InputStream in) {
           try {
                this.bfoto = new byte[(int) fileName.length()];
                //salvando foto no array de bytes
                in.read(bfoto);
                teste = "teste";
                in.close();
                
               
                } catch (IOException e) {
                System.out.println(e.getMessage());
                }
    }*/
    
	public Obra getObra() {
		return this.obra;
	}

	public void setObra(Obra obra) {
		this.obra = obra;
	}

	

	public String getDestinoSalvar() {
		return destinoSalvar;
	}

	public void setDestinoSalvar(String destinoSalvar) {
		this.destinoSalvar = destinoSalvar;
	}

	
	public void setLista(List<Obra> lista) {
		this.lista = lista;
	}

	public Obra getObraSelecionada() {
		return obraSelecionada;
	}

	public void setObraSelecionada(Obra obraSelecionada) {
		this.obraSelecionada = obraSelecionada;
	}

	

	public String getTeste() {
		return teste;
	}

	public void setTeste(String teste) {
		this.teste = teste;
	}

	
}
